//
//  LHMScanImageView.m
//  LittleMingClassmate
//
//  Created by dllo on 16/7/29.
//  Copyright © 2016年 小铭. All rights reserved.
//

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

#import "LHMScanImageView.h"

@implementation LHMScanImageView

static CGRect oldframe;

+(void)scanBigImageWithImageVIew:(UIImageView *)currentImageVIew {
    
    UIImage *image = currentImageVIew.image;
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    oldframe = [currentImageVIew convertRect:currentImageVIew.bounds toView:window];
    [backgroundView setBackgroundColor:[UIColor blackColor]];
    
    [backgroundView setAlpha:0];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:oldframe];
    [imageView setImage:image];
    [imageView setTag:0];
    [backgroundView addSubview:imageView];
    [window addSubview:backgroundView];
    
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideImageView:)];
    [backgroundView addGestureRecognizer:tapGes];
    
    [UIView animateWithDuration:0.4 animations:^{
        CGFloat y, width, height;
        
        y = (HEIGHT - image.size.height * WIDTH / image.size.width) * 0.5;
        
        width = WIDTH;
        
        height = image.size.height * WIDTH / image.size.width;
        
        [imageView setFrame:CGRectMake(0, y, width, height)];
        
        [backgroundView setAlpha:1];
        
    } completion:^(BOOL finished) {
        
    }];
    
}

+(void)hideImageView:(UITapGestureRecognizer *)tap {
    
    UIView *backgroundView = tap.view;
    
    UIImageView *imageView = [tap.view viewWithTag:0];
    
    [UIView animateWithDuration:0.4 animations:^{
        [imageView setFrame:oldframe];
        [backgroundView setAlpha:0];
    } completion:^(BOOL finished) {
        [backgroundView removeFromSuperview];
    }];
    
}

@end
